package org.workforce.util;

import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Utils {

    public static String StringField(String data) {
        if( data == null ) {
            return "";
        } else {
            return data;
        }
    }
    public static long stringToDate(String strDate) {
        Calendar c = Calendar.getInstance();
        try {
            int sd = strDate.indexOf("-", 0);
            int ed = strDate.indexOf("-", sd + 1);
            int en = strDate.indexOf(" ", ed + 1);
            String yy = strDate.substring(0, sd);
            String mm = strDate.substring(sd + 1, ed);
            if (en < 0) {
                en = strDate.length();
            }
            String dd = strDate.substring(ed + 1, en);
            System.out.println("dd[" + dd + "]");
            System.out.println("mm[" + mm + "]");
            System.out.println("yy[" + yy + "]");
            c.set(Calendar.DATE, Integer.parseInt(dd));
            c.set(Calendar.MONTH, Integer.parseInt(mm) - 1);
            c.set(Calendar.YEAR, Integer.parseInt(yy));
            return c.getTimeInMillis();
        } catch (Exception e) {
            System.out.println("Invalid Date Parser Exception ");
            e.printStackTrace();
        }
        return c.getTimeInMillis();
    }

    public static String DateToString(long l_date) {
        Calendar c = Calendar.getInstance();
        try {
            c.setTimeInMillis(l_date);
            String pattern = "00";
            DecimalFormat myFormatter = new DecimalFormat(pattern);
            String dd = myFormatter.format(c.get(Calendar.DATE));
            String mm = myFormatter.format(c.get(Calendar.MONTH));
            pattern = "0000";
            myFormatter = new DecimalFormat(pattern);
            String yy = myFormatter.format(c.get(Calendar.YEAR));
            StringBuffer s = new StringBuffer();
            s.append(mm).append("/").append(dd).append("/").append(yy);
            return s.toString();
        } catch (Exception e) {
            System.out.println("Invalid Date Parser Exception ");
            e.printStackTrace();
        }
        return null;
    }

    public static String getDateDefault() {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        String sd = sf.format(Calendar.getInstance().getTime());
        return sd;
    }

    public static String getBeginDate() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.MONTH, Calendar.MAY);
        c.set(Calendar.YEAR, 2007);
        c.set(Calendar.DATE, 1);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        String sd = sf.format(c.getTime());
        return sd;
    }

    public static String getDateString(String sdate) {
        String sd = null;
        if (sdate != null) {
            if (sdate.length() > 0) {
                sd = sdate;
            } else {
                sd = getDateDefault();
            }
        } else {
            sd = getDateDefault();
        }
        return sd;
    }

    public static String getMsisdn(String msisdn) {
        String m = msisdn.replace('*', '%');
        return m;
    }

    public static String getString(String str) {
        String s = str.replace('*', '%');
        return s;
    }

    public static String getDate() {
        StringBuffer ud = new StringBuffer();
        NumberFormat formatter = new DecimalFormat("00");
        Calendar rightNow = Calendar.getInstance();
        ud.append(formatter.format(rightNow.get(Calendar.DAY_OF_MONTH)));
        ud.append(formatter.format(rightNow.get(Calendar.MONTH) + 1));
        ud.append(formatter.format(rightNow.get(Calendar.YEAR)));
        ud.append(formatter.format(rightNow.get(Calendar.HOUR_OF_DAY)));
        ud.append(formatter.format(rightNow.get(Calendar.MINUTE)));
        ud.append(formatter.format(rightNow.get(Calendar.SECOND)));
        return ud.toString();
    }

    public static String getNumberRandom() {
        // NumberFormat formatter = new DecimalFormat("###########");
        // Random rand = new Random();
        StringBuffer ui = new StringBuffer();
        // ui.append( formatter.format( rand.nextInt(100+1) ) );
        // ui.append( formatter.format( rand.nextInt(200+1) ) );
        // ui.append( formatter.format( rand.nextInt(300+1) ) );
        // ui.append( formatter.format( Math.random() ) );
        // rand = null;
        ui.append(Math.random());
        int i = ui.indexOf(".");
        if (i > -1) {
            return ui.substring(i + 1, i + 4);
        }
        String userid = (ui.toString().length() > 3) ? ui.toString().substring(
                0, 3) : ui.toString();
        return userid;
    }

    public static String generateId() {
        StringBuffer str = new StringBuffer("id");
        str.append(getDate());
        str.append(getNumberRandom());
        return str.toString();
    }

    public static Timestamp getTimestamp() {
        SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String thistime = fm.format(Calendar.getInstance().getTime());
        Timestamp time = Timestamp.valueOf(thistime);

        return time;
    }

    public static void main(String args[]) {
        //long l = Utils.stringToDate("08/24/2007");
        String cid = Utils.generateId();
        System.out.println("id[" + cid + "]");
    }
}
